import unittest

from Autotests.BaseTest import BaseManservSOAPTestCase, PartnerAndAccountAutoCreation


@PartnerAndAccountAutoCreation
class CustomColumn(BaseManservSOAPTestCase):
    def test_AddCustomColumn(self):
        columnName = 'autoCustColumn'
        numberOfCustomColumnsBefore = self._getCustomColumnsCount()
        result = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), columnName)
        numberOfCustomColumnsAfter = self._getCustomColumnsCount()
        #verify
        self.assertTrue(result.isnumeric())
        self.assertEqual(numberOfCustomColumnsBefore + 1, numberOfCustomColumnsAfter)
        self.assertEqual(columnName, self.executor.getCustomColumnInfoById(result).name)
        #cleaning
        self.executor.removeCustomColumn(result)

    def test_ModifyCustomColumn(self):
        newColumnName = 'autoNewColumnName'
        custColumnId = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), 'autoCustColumnForModification')
        numberOfCustomColumnsBefore = self._getCustomColumnsCount()
        self.executor.modifyCustomColumn(custColumnId, newColumnName)
        numberOfCustomColumnsAfter = self._getCustomColumnsCount()
        #verify
        self.assertEqual(numberOfCustomColumnsBefore, numberOfCustomColumnsAfter)
        self.assertEqual(newColumnName, self.executor.getCustomColumnInfoById(custColumnId).name)
        #cleaning
        self.executor.removeCustomColumn(custColumnId)

    def test_RemoveCustomColumn(self):
        custColumnId = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), 'autoCustColumnForDel')
        numberOfCustomColumnsBefore = self._getCustomColumnsCount()
        self.executor.removeCustomColumn(custColumnId)
        numberOfCustomColumnsAfter = self._getCustomColumnsCount()
        #verify
        self.assertEqual(numberOfCustomColumnsBefore - 1, numberOfCustomColumnsAfter)

    def test_EnumerateCustomColumn(self):
        columnName = 'autoCustColumnForEnum'
        custColumnId = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), columnName)
        enum = self.executor.enumerateCustomColumns(self.getPartnerForCaseInfo())
        #verify
        self.assertIsInstance(enum, self.manserv.createObject('mansvc:ArrayOfCustomColumnInfo').__class__)
        self._checkCustomColumnInfo(columnName, custColumnId, enum.column[0])
        #cleaning
        self.executor.removeCustomColumn(custColumnId)

    def test_GetCustomColumnInfoById(self):
        columnName = 'autoCustColumnForGetter'
        custColumnId = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), columnName)
        custColumnInfo = self.executor.getCustomColumnInfoById(custColumnId)
        #verify
        self._checkCustomColumnInfo(columnName, custColumnId, custColumnInfo)

        self.executor.removeCustomColumn(custColumnId)
        #verify
        with self.assertRaises(Exception) as exc:
            self.executor.getCustomColumnInfoById(custColumnId)
        self.assertEqual(exc.exception.fault.detail.operationResult.errorCode, '1800')
        self.assertEqual(exc.exception.fault.detail.operationResult.errorMessage,
                         'Custom column #%s not found in the database' % custColumnId)

    def test_CustomColumnValues(self):
        customValue = 'value1'
        customValueSecond = 'value2'

        customColumnId = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), 'newCustomColumn')
        customColumnIdSecond = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), 'customColumnSecond')

        customColumnValues = self.executor.getAccountCustomColumnValues(self.getAccountForCaseInfo().accountInfo)
        self.assertEqual(customColumnValues, '')

        values = self.manserv.createObject('mansvc:ArrayOfCustomColumnValue')
        values.columnValue = [self._createCustomColumnValue(customColumnId, customValue),
                              self._createCustomColumnValue(customColumnIdSecond, customValueSecond), ]

        self.executor.updateAccountCustomColumnValues(self.getAccountForCaseInfo().accountInfo, values)
        customColumnValues = self.executor.getAccountCustomColumnValues(self.getAccountForCaseInfo().accountInfo)
        self.assertEqual(len(customColumnValues.columnValue), 2)
        self.assertEqual(customColumnValues.columnValue[0].customColumnId, customColumnId)
        self.assertEqual(customColumnValues.columnValue[0].value, customValue)
        self.assertEqual(customColumnValues.columnValue[1].customColumnId, customColumnIdSecond)
        self.assertEqual(customColumnValues.columnValue[1].value, customValueSecond)

    def test_CustomColumnValuesById(self):
        customValue = 'value1'
        customValueSecond = 'value2'

        customColumnId = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), 'newCustomColumn')
        customColumnIdSecond = self.executor.addCustomColumn(self.getPartnerForCaseInfo(), 'customColumnSecond')

        customColumnValues = self.executor.getAccountCustomColumnValuesById(self.getAccountForCaseInfo().accountInfo)
        self.assertEqual(customColumnValues, '')

        values = self.manserv.createObject('mansvc:ArrayOfCustomColumnValue')
        values.columnValue = [self._createCustomColumnValue(customColumnId, customValue),
                              self._createCustomColumnValue(customColumnIdSecond, customValueSecond), ]

        self.executor.updateAccountCustomColumnValuesById(self.getAccountForCaseInfo().accountInfo, values)
        customColumnValues = self.executor.getAccountCustomColumnValuesById(self.getAccountForCaseInfo().accountInfo)
        self.assertEqual(len(customColumnValues.columnValue), 2)
        self.assertEqual(customColumnValues.columnValue[0].customColumnId, customColumnId)
        self.assertEqual(customColumnValues.columnValue[0].value, customValue)
        self.assertEqual(customColumnValues.columnValue[1].customColumnId, customColumnIdSecond)
        self.assertEqual(customColumnValues.columnValue[1].value, customValueSecond)

    def _checkCustomColumnInfo(self, columnName, custColumnId, custColumnInfo):
        self.assertIsInstance(custColumnInfo, self.manserv.createObject('mansvc:CustomColumnInfo').__class__)
        self.assertEqual(custColumnInfo.id, custColumnId)
        self.assertEqual(custColumnInfo.partnerId, self.getPartnerForCaseInfo().id)
        columnFieldType = self.manserv.createObject('mansvc:ColumnFieldType')
        self.assertEqual(custColumnInfo.valueType, columnFieldType.String)
        self.assertEqual(custColumnInfo.valueType, columnFieldType.String)
        self.assertEqual(custColumnInfo.name, columnName)

    def _createCustomColumnValue(self, columnId, value):
        columnValue = self.manserv.createObject('mansvc:CustomColumnValue')
        columnValue.customColumnId = columnId
        columnValue.value = value
        return columnValue

    def _getCustomColumnsCount(self):
        result = self.executor.enumerateCustomColumns(self.getPartnerForCaseInfo())
        if result == '':
            return 0
        return len(result.column)


if __name__ == "__main__":
    unittest.main()
